//获取应用实例
var app = getApp()

var CommonUtils = app.CommonUtils;
var Keys = require("../../assets/data/keys.js")
const Toptips = require('../../dist/toptips/index');

Page(Object.assign({}, Toptips, {
  data: {
    addressBean: {

    },
    address: {},
    provinces: [],
    province: '北京',
    citys: [],
    city: '北京市',
    countys: [],
    county: '东城区',
    value: [0, 0, 0],
    values: [0, 0, 0],
    isDefault: 0,      //是否是默认地址
    haid: '',
    isEditAddress: false,
    isCheck: false,
  },

  checkboxChange: function (e) {
    if (this.data.isDefault == 0) {
      this.setData({
        isDefault: 1
      })
    } else {
      this.setData({
        isDefault: 0
      })
    }
  },

  bindChange: function (e) {
    var val = e.detail.value
    var t = this.data.values;
    var cityData = this.data.addressBean;

    this.setData({
      isSelectAddress: true,
    })

    if (val[0] != t[0]) {
      const citys = [];
      const countys = [];
      for (let i = 0; i < cityData[val[0]].erji.length; i++) {
        citys.push(cityData[val[0]].erji[i].city)
      }
      for (let i = 0; i < cityData[val[0]].erji[0].sanji.length; i++) {
        countys.push(cityData[val[0]].erji[0].sanji[i].District)
      }
      this.setData({
        province: this.data.provinces[val[0]],
        city: cityData[val[0]].erji[0].city,
        citys: citys,
        county: cityData[val[0]].erji[0].sanji[0].District,
        countys: countys,
        values: val,
        value: [val[0], 0, 0]
      })
      return;
    }
    if (val[1] != t[1]) {
      const countys = [];
      for (let i = 0; i < cityData[val[0]].erji[val[1]].sanji.length; i++) {
        countys.push(cityData[val[0]].erji[val[1]].sanji[i].District)
      }
      this.setData({
        city: this.data.citys[val[1]],
        county: cityData[val[0]].erji[val[1]].sanji[0].District,
        countys: countys,
        values: val,
        value: [val[0], val[1], 0]
      })
      return;
    }
    if (val[2] != t[2]) {
      this.setData({
        province: this.data.provinces[val[0]],
        city: this.data.citys[val[1]],
        county: this.data.countys[val[2]],
        values: val
      })
      return;
    }
  },

  open: function () {
    this.setData({
      condition: !this.data.condition
    })
  },

  bindCancel: function () {
    wx.navigateBack({})
  },

  bindSave: function (e) {
    var _this = this;
    var linkMan = e.detail.value.linkMan;
    var address = e.detail.value.address;
    var mobile = e.detail.value.mobile;
    var isDefault = e.detail.value.isDefault;
    if (linkMan == "") {
      _this.showZanTopTips("请填写联系人姓名");
      return
    }
    if (!CommonUtils.isPoneAvailable(mobile)) {
      _this.showZanTopTips("请输入有效的手机号");
      return;
    }
    if (address == "") {
      _this.showZanTopTips("请填写详细地址");
      return
    }
    if (_this.data.isEditAddress) {
      wx.request({
        url: Keys.data.url_Address_Update,
        data: {
          DataType: "Address_Update",
          haid: _this.data.haid,
          name: linkMan,
          tel: mobile,
          province: _this.data.province,
          city: _this.data.city,
          district: _this.data.county,
          address: address,
          isture: _this.data.isDefault,
          uid: app.login_id
        },
        success: function (res) {
          if (res.data.isok == "true") {
            wx.showToast({ title: '保存成功' })
            setTimeout(function () { wx.navigateBack() }, 2000)
          } else { _this.showZanTopTips(res.data.info); }
        },
        fail: function () { _this.showZanTopTips("网络错误"); }
      })
    } else {
      wx.request({
        url: Keys.data.url_Address_Add,
        data: {
          DataType: "Address_Add",
          name: linkMan,
          tel: mobile,
          province: _this.data.province,
          city: _this.data.city,
          district: _this.data.county,
          address: address,
          isture: _this.data.isDefault,
          uid: app.login_id
        },
        success: function (res) {
          if (res.data.isok == "true") {
            wx.showToast({ title: '保存成功' })
            setTimeout(function () { wx.navigateBack() }, 2000)
          } else { _this.showZanTopTips(res.data.info); }
        },
        fail: function () { _this.showZanTopTips("网络错误"); }
      })
    }
  },

  onLoad: function (e) {
    var _this = this;
    _this.initAddress();
    if (e.haid != null) {
      wx.request({
        url: Keys.data.url_Address_Detail,
        data: {
          DataType: "Address_Detail",
          haid: e.haid,
        },
        success: function (res) {
          if (res.data.isok == "true") {
            _this.setData({
              haid: e.haid,
              isEditAddress: true,
              province: res.data.data.HA_Province,
              city: res.data.data.HA_City,
              county: res.data.data.HA_District,
              address: res.data.data,
              isDefault: res.data.data.HA_Istrue == "0" ? "0" : "1",
              isCheck: res.data.data.HA_Istrue == "0" ? false : true,
            })
          } else { CommonUtils.showToastWithImg(res.data.info, 2) }
        }
      })
    }
  },

  /**
   * 请求选择的地址数据
   */
  initAddress: function () {
    var _this = this;
    wx.showLoading()
    wx.request({
      url: Keys.data.url_City,
      success: function (res) {
        if (res.data.isok == "true") {
          _this.initAddressData(res.data.datas)
        } else {
          _this.showZanTopTips(res.data.info);
        }
      },
      complete: function () { wx, wx.hideLoading() },
      fail: function () { _this.showZanTopTips("网络错误"); }
    })
  },

  /**
   * 初始化地址选择数据
   */
  initAddressData: function (datas) {
    const provinces = [];
    const citys = [];
    const countys = [];

    for (let i = 0; i < datas.length; i++) {
      provinces.push(datas[i].yiji);
    }
    for (let i = 0; i < datas[0].erji.length; i++) {
      citys.push(datas[0].erji[i].city)
    }
    for (let i = 0; i < datas[0].erji[0].sanji.length; i++) {
      countys.push(datas[0].erji[0].sanji[i].District)
    }
    this.setData({
      addressBean: datas,
      provinces: provinces,
      citys: citys,
      countys: countys,
    })
  }
}))
